package com.example.pkmedical.Utils;


import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;

import java.util.Locale;

/**
 * Created by Administrator on 2019/img_cg_on/13.
 */

public class LanguageUtil {
    //更改App语言
    public static void changeAppLanguage(Context context, Locale locale) {
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        Configuration configuration = resources.getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }
        resources.updateConfiguration(configuration, metrics);
    }

    public static boolean isSameWithSetting(Context context) {
        String lang = context.getResources().getConfiguration().locale.getLanguage();
        return lang.equals(getAppLocale(context).getLanguage());
    }

    public static Locale getAppLocale(Context context) {
        Locale locale = null;
        switch (getLgType(context)) {
            case 0:
                //简体中文
                locale = Locale.SIMPLIFIED_CHINESE;
                break;
            case 1:
                //英语
                locale = LocaleUtils.YINGYU;
                break;
            case 2:
                //法语
                locale = LocaleUtils.FAYU;
                break;
            case 3:
                //巴西
                locale = LocaleUtils.BAXI;
                break;
            case 4:
                //葡萄牙
                locale = LocaleUtils.PUTAOYA;
                break;
            case 5:
                //西班牙
                locale = LocaleUtils.XIBANYA;
                break;
            case 6:
                //阿拉伯
                locale = LocaleUtils.ALABO;
                break;
            case 7:
                //俄罗斯
                locale = LocaleUtils.ELUOSI;
                break;
            case 8:
                //泰国
                locale = LocaleUtils.TAIGUO;
                break;
            case 9:
                //韩国
                locale = LocaleUtils.HANGUO;
                break;
            case 10:
                //日本
                locale = LocaleUtils.RIBEN;
                break;
            case 11:
                //德国
                locale = LocaleUtils.DEGUO;
                break;
            case 12:
                //意大利
                locale = LocaleUtils.YIDALI;
                break;
            case 13:
                //罗马尼亚
                locale = LocaleUtils.LUOMANIYA;
                break;
        }
        return locale;
    }

    public static int getLgType(Context context) {
        int lgType;
        SharedPreferences sharedPreferences = context.getSharedPreferences("user",
                Activity.MODE_PRIVATE);
        String lgStr = sharedPreferences.getString("lgStr", "");
        if (lgStr != null && !lgStr.equals("")) {
            lgType = Integer.parseInt(lgStr);
        } else {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("lgStr", "0");
            editor.commit();
            lgType = 0;
        }
        return lgType;
    }

}